﻿using CoreShop.Library;
using CoreShop.Model;
using CoreShop.Repository;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Yitter.IdGenerator;

namespace CoreShop.Services
{
    /// <summary>
    /// 接口实现
    /// </summary>
    public class corecmsprintschemedetailServices : Repository<corecmsprintschemedetail>, IcorecmsprintschemedetailServices
    {
        public bool InsertAsync(corecmsprintschemedetail entity)
        {
            return base.Change<corecmsprintschemedetail>().Insert(entity);
        }
        public bool InsertOrUpdate(List<corecmsprintschemedetail> entity)
        {
            return base.InsertOrUpdate(entity);
        }
        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(corecmsprintschemedetail entity)
        {
            return base.Change<corecmsprintschemedetail>().Update(a => new corecmsprintschemedetail()
            {
                printSchemeId = entity.printSchemeId,
                templateId = entity.templateId,
                billtypeId = entity.billtypeId
            }, a => a.id == entity.id);
        }

        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(List<corecmsprintschemedetail> entity)
        {
            bool returnKey = true;
            foreach (var item in entity)
            {
                returnKey = base.Change<corecmsprintschemedetail>().Update(a => new corecmsprintschemedetail()
                {
                    printSchemeId = item.printSchemeId,
                    templateId = item.templateId,
                    billtypeId = item.billtypeId,
                }, a => a.id == item.id);
            }
            return returnKey;
        }

        /// <summary>
        /// 重写删除指定ID的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteByIdAsync(long id)
        {
            return base.Change<corecmsprintschemedetail>().DeleteById(id);
        }
        /// <summary>
        /// 重写删除指定ID集合的数据(批量删除)
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteByIdsAsync(int[] ids)
        {
            bool returnKey = true;
            foreach (int id in ids)
            {
                returnKey = base.Change<corecmsprintschemedetail>().DeleteById(id);
            }
            return returnKey;
        }

        #region 获取缓存的所有数据==========================================================
        /// <summary>
        /// 获取缓存的所有数据
        /// </summary>
        /// <returns></returns>
        public List<corecmsprintschemedetail> GetCaChe()
        {
            return base.GetList();
        }
        #endregion


        #region 根据条件查询分页数据
        /// <summary>
        /// 根据条件查询分页数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsprintschemedetail> QueryPageAsync(ref int count, int? pageIndex, int? pageSize, string sName)
        {
            List<corecmsprintschemedetail> custos = new List<corecmsprintschemedetail>();
            var where = Expressionable.Create<corecmsprintschemedetail>();
            //if (!string.IsNullOrEmpty(sName))
            //{
            //    where = where.And(a => a.printName.Contains(sName));
            //}
            if (pageIndex >= 1 && pageSize > 0)
            {
                custos = base.AsQueryable().WhereIF(true, where.ToExpression())
                    .ToPageList((int)pageIndex, (int)pageSize, ref count)
                    .OrderBy(a => a.id).ToList();
            }
            else
            {
                custos = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.id).ToList();
            }
            return custos;
        }
        #endregion

        #region
        /// <summary>
        /// 根据条件查询数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsprintschemedetail> QueryByPrintSchemeId(long id)
        {
            var list = base.AsQueryable().LeftJoin<corecmsprintbilltemplate>((d, t) => d.templateId == t.id)
                .Where((d, t) => d.printSchemeId == id)
                .Select((d, t) => new corecmsprintschemedetail
                {
                    id = d.id,
                    printSchemeId = d.printSchemeId,
                    templateId = d.templateId,
                    billtypeId = d.billtypeId,
                    foodPrintType = t.foodPrintType,
                    printCount = d.printCount,
                    selectedFlag = d.selectedFlag,
                    templateName = t.name,
                    foodSort = d.foodSort
                }).ToList();
            return list;
        }
        #endregion

        #region 根据方案查询方案下的打印模板
        /// <summary>
        /// 根据方案查询方案下的打印模板(billType=1结账单、预结账单、客户点菜单,收银交班单，会员充值小票,2为厨房打印
        /// </summary>
        /// <returns></returns>
        public List<corecmsprintschemedetail> QueryPrintBilltemplate(corecmsprintscheme printscheme, int billTypeTag)
        {
            //billType=1本地打印用，只加载结账单、预结账单、客户点菜单,收银交班单，会员充值小票
            //billType=2为厨房打印
            var where = Expressionable.Create<corecmsprintschemedetail>();
            where = where.And(a => a.printSchemeId == printscheme.id);
            where = where.And(a => a.selectedFlag == true);

            var billtypes = new corecmsprintbilltypeServices().AsQueryable().Where(p=>p.tagId == billTypeTag).ToList();
            var billtypeIds = billtypes.Select(p=> p.id).ToList();

            var schemedetails = new corecmsprintschemedetailServices().AsQueryable()
                .Where(p => p.printSchemeId == printscheme.id && billtypeIds.Contains(p.billtypeId)).ToList();

            var templateIds = schemedetails.Select(p => p.templateId).ToList();
            var billtemplateList = new corecmsprintbilltemplateServices().AsQueryable().Where(p => templateIds.Contains(p.id)).ToList();

            //标签打印
            var schemeLabels = new corecmsprintschemedetailServices().AsQueryable()
                .Where(p => p.billtypeId == (int)GlobalEnumVars.PrintbillType.Label && p.selectedFlag == true).ToList();
            schemedetails.AddRange(schemeLabels);

            foreach (var schemedetail in schemedetails)
            {
                var billtemplate = billtemplateList.Find(p => p.id == schemedetail.templateId);
                if (billtemplate != null)
                {
                    billtemplate.printCount = schemedetail.printCount;
                    billtemplate.printerName = printscheme.printerName;
                    schemedetail.billtemplate = billtemplate;
                    billtemplate.foodPackPrintType = schemedetail.foodPackPrintType;
                }
            }

            return schemedetails;
        }
        #endregion

        public List<corecmsprintschemedetail> QueryByTemplateAsync(int templateId)
        {
            var list = base.AsQueryable().LeftJoin<corecmsprintbilltemplate>((d, t) => d.templateId == t.id)
                .Where((d, t) => d.templateId == templateId)
                .Select((d, t) => new corecmsprintschemedetail
                {
                    id = d.id,
                    printSchemeId = d.printSchemeId,
                    templateId = d.templateId,
                    billtypeId = d.billtypeId,
                    foodPrintType = t.foodPrintType,
                    printCount = d.printCount,
                    selectedFlag = d.selectedFlag,
                    templateName = t.name,
                    foodSort = d.foodSort
                }).ToList();
            return list;
        }
        /// <summary>
        /// 根据打印方案id和打印类型id获取单条
        /// </summary>
        /// <param name="printSchemeId"></param>
        /// <param name="billtypeId"></param>
        /// <returns></returns>
        public corecmsprintschemedetail SelectById(int printSchemeId, int billtypeId)
        {
            return base.Change<corecmsprintschemedetail>().GetFirst(a => a.printSchemeId == printSchemeId && a.billtypeId == billtypeId);
        }

        public List<corecmsprintschemedetail> SelectByTemplateId(int templateId)
        {
            return base.Change<corecmsprintschemedetail>().GetList(p => p.templateId == templateId);
        }

        public List<corecmsprintschemedetail> SelectByPrintSchemeId(int printSchemeId)
        {
            return base.Change<corecmsprintschemedetail>().GetList(p => p.printSchemeId == printSchemeId);
        }

        public List<corecmsprintschemedetail> SelectByBilltypeId(int billtypeId)
        {
            var list = base.Change<corecmsprintschemedetail>().GetList(p => p.billtypeId == billtypeId && p.selectedFlag == true);
            if ((list == null) || (list.Count == 0)) return null;

            //打印模板
            var templateIds = list.Select(p => p.templateId).ToList();
            var billtemplateList = new corecmsprintbilltemplateServices().GetList(p => templateIds.Contains(p.id));
           
            //打印方案
            var printschemeIds = list.Select(p => p.printSchemeId).ToList();
            var printschemeList = new corecmsprintschemeServices().GetList(p => printschemeIds.Contains(p.id));

            foreach (var item in list)
            {
                item.billtemplate = billtemplateList.Find(p => p.id == item.templateId);
                item.printscheme = printschemeList.Find(p => p.id == item.printSchemeId);
                if ((item.printscheme != null) && (item.billtemplate != null))
                {
                    item.billtemplate.printerName = item.printscheme.printerName;
                }
            }
            return list;
        }

    }
}